package cn.pegasus.teamwork.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.sysconfig.entity.SysCompanyInterfaceInfo;
import cn.pegasus.teamwork.sysconfig.service.SysCompanyInterfaceInfoService;

/**
 * @ClassName: SysDictionaryController
 * @Description: 字典控制器
 * @author buyuer
 * @date 2015年11月25日 上午10:46:10
 * 
 */
@RequestMapping("/sysCom/*")
@Controller
public class SysComInterfaceController {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(SysComInterfaceController.class);
	@Autowired
	private SysCompanyInterfaceInfoService service;
   
	@RequestMapping("/comInterfaceList")
	public ModelAndView dicTopList(@RequestParam Map<String, Object> map) {
		ModelAndView andView = new ModelAndView("system/cominterface-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try {

		PageObject<SysCompanyInterfaceInfo> dicList = service.Pagequery(map);
		andView.addObject("dicList", dicList);
		
		} catch (Exception e) {
			LOGGER.error("查询异常", e);
			e.printStackTrace();
		}
		return andView;
	}

	@RequestMapping("/addOrUpdate")
    public ModelAndView goAddOrUpdDic(@RequestParam(value = "id", required = false) Integer id){
        ModelAndView andView = null;
        if (id != null) {
        	Map<String,Object> paramsMap = new HashMap<String,Object>();
        	paramsMap.put("id", id);
        	SysCompanyInterfaceInfo companyInfo = service.detail(paramsMap);
            if (companyInfo != null) {
                andView = new ModelAndView("system/com-upd");
                andView.addObject("com", companyInfo);
            } else {
                andView = new ModelAndView("404");
            }
        } else {
        	
            andView = new ModelAndView("system/com-add");
          
        }
        return andView;
    }
	
	
	@ResponseBody
	@RequestMapping("addComInterfaceInfo")
	public Map<String, Object> addDic(
			@RequestParam(value = "url", required = true) String url,
			@RequestParam(value = "token", required = true) String token,
			@RequestParam(value = "sponsorName", required = true) String sponsorName,
			@RequestParam(value = "taskUrl", required = true) String taskUrl,
			@RequestParam(value = "sponsorCode", required = true) String sponsorCode) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Map<String,Object> params = new HashMap<String,Object>();
			//通过赞助商code来判断唯一性
			params.put("sponsorCode", sponsorCode);
			SysCompanyInterfaceInfo info = service.detail(params);
			//没有重复
			if (null==info) {
				SysCompanyInterfaceInfo interfaceInfo = new SysCompanyInterfaceInfo();
				interfaceInfo.setSponsorCode(sponsorCode);
				interfaceInfo.setToken(token);
				interfaceInfo.setUrl(url);
				interfaceInfo.setSponsorName(sponsorName);
				interfaceInfo.setTaskUrl(taskUrl);
				service.add(interfaceInfo);
				resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE,
						null);
			//有重复
			} else {
				resultMap = ReMessage.resultBack(ParameterEunm.ERROR_403_CODE,
						null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("添加赞助商接口信息失败");
			resultMap = ReMessage
					.resultBack(ParameterEunm.ERROR_500_CODE, null);
		}
		return resultMap;
	}

	@ResponseBody
    @RequestMapping("delCom")
    public Map<String, Object> delDic(@RequestParam(value = "id") Integer id){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	SysCompanyInterfaceInfo info = new SysCompanyInterfaceInfo();
        	info.setId(id);
            service.delete(info);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            resultMap = ReMessage.resultBack(ParameterEunm.FAILED_CODE, null);
            LOGGER.error("锁定失败." , e);
        } catch (Throwable e) {
            resultMap = ReMessage.resultBack(ParameterEunm.FAILED_CODE, null);
            LOGGER.error("锁定失败." , e);
        }
        return resultMap;
    }
	
	
	
	@ResponseBody
	@RequestMapping("updComInterfaceInfo")
	public Map<String, Object> updCom(@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if(null!=map.get("id") && null!=map.get("sponsorCode")){
				Integer id = Integer.parseInt(map.get("id").toString());
				String sponsorCode = map.get("sponsorCode").toString();
				Map<String,Object> params = new HashMap<String,Object>();
				List<SysCompanyInterfaceInfo> listInfo = service.query(params);
				if(null!=listInfo && listInfo.size()>0){
					for(SysCompanyInterfaceInfo info : listInfo){
						//修改后的值有重复
						if(!info.getId().equals(id) && sponsorCode.equals(info.getSponsorCode())){
							resultMap = ReMessage
									.resultBack(ParameterEunm.ERROR_500_CODE, null);
							resultMap.put("message", "修改后的值又重复！");
							return resultMap;
						}
					}
				}
				service.update(map);
				resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE,
						null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("修改赞助商接口信息失败");
			resultMap = ReMessage
					.resultBack(ParameterEunm.ERROR_500_CODE, null);
		}
		return resultMap;
	}
}
